package com.example.demo.controller;

import com.example.demo.dto.ResponseResult;
import com.example.demo.service.DynamicUrlService;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloController {

    @Resource
    private DynamicUrlService dynamicUrlService;

    @RequestMapping("/getPath")
    public ResponseResult<String> getPath(String userId, String goodId){
        if ( (userId==null||userId=="")
                || (goodId==null||goodId=="") ){
            throw new RuntimeException("userId或者goodId不能为空");
        }

        String path = dynamicUrlService.createPath(userId,goodId);

        ResponseResult<String> result=new ResponseResult<>();
        result.setCode("000000");
        result.setStatus("success");
        result.setData(path);

        return result;
    }

    @RequestMapping("/{path}/hello")
    public ResponseResult<String> hello(@PathVariable("path") String path,
                                        String userId, String goodId,
                                        String name, Integer age){
        System.out.println(userId+"  "+goodId);
        System.out.println(path);

        String key=userId+goodId;
        if (!dynamicUrlService.checkPath(key,path)){
            throw new RuntimeException("path 检验出错");
        }

        System.out.println(name+"  "+age);

        ResponseResult<String> result=new ResponseResult<>();
        result.setCode("000000");
        result.setStatus("success");
        result.setData(name+"  "+age);

        return result;
    }

    @ExceptionHandler
    public ResponseResult<String> handleException(Exception e){
        ResponseResult<String> result=new ResponseResult<>();
        result.setCode("111111");
        result.setStatus("error");
        result.setMessage(e.getMessage());

        return result;
    }
}
